<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改':'添加'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="82px">
        <el-form-item
          label="工号:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.psncode"
            :disabled="isUpdate"
            placeholder="请输入"
            clearable/>
        </el-form-item>
        <el-form-item
          label="姓名:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.psnname"
            placeholder="请输入"
            clearable/>
        </el-form-item>
        <el-form-item
          label="邮箱:"
          prop="name">
          <el-input
            :maxlength="50"
            v-model="form.email"
            placeholder="请输入"
            clearable/>
        </el-form-item>
        <el-form-item
          label="手机号码:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.phone"
            placeholder="请输入"
            clearable/>
        </el-form-item>
        <el-form-item
          label="公司单元:"
          prop="name">
          <el-input
            :maxlength="50"
            v-model="form.orgName"
            placeholder="请输入"
            />
        </el-form-item>
        <el-form-item
          label="单元码:"
          prop="name">
          <el-select
            :maxlength="20"
            v-model="form.orgCode"
            placeholder="请输入">
            <el-option label="营销中心1925" value="1925"/>
            <el-option label="二部总部1183" value="1183"/>
            <el-option label="招商联合1528" value="1528"/>
            <el-option label="医药1529" value="1529"/>
            <el-option label="医药1178" value="1178"/>
            <el-option label="天一1173" value="1173"/>
          </el-select>
        </el-form-item>
        <el-form-item label="免签:" prop="noSignCard">
          <el-select
            :maxlength="20"
            v-model="form.noSignCard"
            placeholder="请输入">
            <el-option label="1" value="1"/>
            <el-option label="0" value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item label="邮件通知:" prop="needInform">
          <el-select
            :maxlength="20"
            v-model="form.needInform"
            placeholder="请输入">
            <el-option label="1" value="1"/>
            <el-option label="0" value="0"/>
          </el-select>
        </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'PsnInfoEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        psncode: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        psnname: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        orgName: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        orgCode: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        noSignCard: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        repository: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        needInform: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/psninfo/edit' : '/psninfo/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
